import Cookie from "js-cookie";
import { post } from "@/utils/http";
import router from "@/router";
import { resetStore } from "@/store";
export default {
  state() {
    return {
      user: {}, //用户信息
    };
  },
  getters: {},
  mutations: {
    setUser(state, user) {
      state.user = user;
    },
    logout(state) {
      // 退出登录的操作
      let data = {
        _mt: "logout",
        _gp: "admin",
      };
      // 发送请求
      post(data)
        .then((res) => {
          // 删除token
          Cookie.remove("token");
          //重置用户信息
          resetStore();
          // 提示
          ElMessage.success("退出登录成功");
          // 跳转
          router.replace({ name: "login" });
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
  actions: {
    setUser(context, user) {
      context.commit("setUser", user);
    },
    logout(context) {
      context.commit("logout");
    },
  },
};
